package com.authine.cloudpivot.ext.dto.model.invoice;

import com.authine.cloudpivot.ext.utils.StringUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.IOException;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Optional;

/**
 * 开票明细拓展字段
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class InvoiceExtendsAttr implements Serializable {

    private static final long serialVersionUID = -5504448482152035996L;
    /**
     * 当前开票税率
     */
    private String taxRate;
    /**
     * 审批结果
     */
    private String approvalReason;
    /**
     * 审批理由
     */
    private String reason;
    /**
     * 是否存在分批申请
     */
    private Boolean ifApprove;
    /**
     * 蓝票对应的多个红票ID列表
     */
    private String offsetIds;
    /**
     * 泛微开票推送值
     */
    private String thfwPunchoutValue;
    /**
     * 实际到货时间
     */
    private String arrivalTime;
    /**
     * 发票签收单图片
     */
    private String image;
    /**
     * 是否小规模纳税人,如果为true是小规模否则不是
     */
    private Boolean ifSmallTaxpayer;
    /**
     * 发票附件个数
     */
    private Integer attachments;

    public static Optional<InvoiceExtendsAttr> conversion(String value) {

        ObjectMapper objectMapper = new ObjectMapper();
        try {
            if (StringUtils.isBlank(value)) {
                return Optional.empty();
            }
            return Optional.ofNullable(objectMapper.readValue(value, InvoiceExtendsAttr.class));
        } catch (IOException ex) {
            return Optional.empty();
        }
    }

    public static Optional<String> conversion(InvoiceExtendsAttr value) {

        ObjectMapper objectMapper = new ObjectMapper();
        try {

            return Optional.ofNullable(objectMapper.writeValueAsString(value));
        } catch (IOException ex) {
            return Optional.empty();
        }
    }

    public static String getDefault() {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return objectMapper.writeValueAsString(InvoiceExtendsAttr.builder().ifApprove(false).build());
        } catch (JsonProcessingException e) {
           e.printStackTrace();
        }
        return null;
    }
}
